package com.plug.controller;

import com.plug.common.consts.Code;
import com.plug.common.model.JsonResult;
import com.plug.controller.util.AbstractController;
import com.plug.service.IPlugBackupRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by admin on 2017/1/5.
 */
@Controller
@RequestMapping("/backupRecord")
public class BackupRecordController extends AbstractController {
    private final static int SUCCESS=0;
    private final static int FALSE=1;


    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private  IPlugBackupRecordService plugBackupRecordservice;

    @RequestMapping(value = "/loadPage", method= RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Object> backup (@RequestParam("page") Integer page,
                                          @RequestParam("size") Integer size,
                                          @RequestParam("actionType")String actionType) {

//       logger.info("------------------------------------offset:{},limit:{}", page, rows);
        page = page == 0 ? 1 :page;

        JsonResult jsonResult = new JsonResult();
        List<Map<String, Object>> listData = new ArrayList<>();
        listData.clear();
        int total=plugBackupRecordservice.findTotal(actionType);
        listData=plugBackupRecordservice.findByPageNum((page - 1) * size, size,actionType);
        jsonResult.putData("data", listData);
        jsonResult.putData("total", total);
        jsonResult.putData("page", page);
        jsonResult.putData("size",size);
        jsonResult.putData("actionType", actionType);
        jsonResult.setCode(Code.OK);
        return  new ResponseEntity<Object>(jsonResult, HttpStatus.OK);
    }



    /**
     *
     * @return
     */
    @RequestMapping(value = "/loadActionType", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> findActionType(){

        Map<String, Object> resultMap = new HashMap<>();
        List<Map<String, Object>> listMap=new ArrayList<>();
        List<Map<String, Object>> list = plugBackupRecordservice.findActionType();
        for (int i = 0; i < list.size(); i++) {
            Map<String, Object> maps=new HashMap<>();
            maps.put("text",list.get(i).get("action_type"));
            maps.put("value",list.get(i).get("id"));
            listMap.add(maps);
        }
        resultMap.put("data",listMap);
        return  resultMap;
    }

    @RequestMapping(value = "/test")
    @ResponseBody
    public Map<String,Object> test(){
        Integer page=1,size=25;
        String actionType="即时任务_:src(task_test)->tgt(task_test)_1484884795289";
        Map<String,Object> map=new HashMap<>();
        map.put("page",page);
        map.put("size",size);
        map.put("actionType",actionType);
        List<Map<String, Object>> list = plugBackupRecordservice.test(map);
        System.out.println(list);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("data",list);
        return resultMap;
    }

}
